Skip to content

Implement automatic dispose code generation#100

Open
konard wants to merge 4 commits intomainfrom
issue-2-3a6319fd
Open

Implement automatic dispose code generation#100
konard wants to merge 4 commits intomainfrom
issue-2-3a6319fd

Conversation

@konard
Copy link
Member

@konard konard commented Sep 14, 2025

Summary

Implements automatic dispose code generation feature as requested in issue #2. This feature allows classes inheriting from DisposableBase to automatically generate disposal code for all disposable fields using C# source generators.

Key Features

  • AutoDispose Attribute: Apply [AutoDispose] to classes that need automatic disposal
  • Source Generator: Compile-time code generation with zero runtime overhead
  • Smart Field Detection: Automatically finds all fields implementing IDisposable or Platform.Disposables.IDisposable
  • Readonly Field Support: Correctly handles readonly fields by skipping null assignment
  • Null-Safe Disposal: Uses null-conditional operator (?.) for safe disposal

Usage Example

[AutoDispose]
public partial class MyClass : DisposableBase
{
    private readonly FileStream _readOnlyField;
    private MemoryStream _writableField;
    
    // Generated Dispose method will automatically:
    // - Call _readOnlyField?.Dispose()
    // - Call _writableField?.Dispose()
    // - Set _writableField = null (non-readonly only)
}

Changes Made

  • Added AutoDisposeAttribute class for marking classes
  • Created AutoDisposeSourceGenerator with full compile-time analysis
  • Added comprehensive test suite with edge cases
  • Included documentation and examples
  • Updated project version to 0.5.0
  • Fixed compatibility issue with existing tests (net7 → net8)

Test Plan

  • All existing tests pass
  • New source generator tests verify functionality
  • Tests cover readonly fields, null fields, and disposal order
  • Verified compile-time code generation works correctly

🤖 Generated with Claude Code


Resolves #2

Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: #2
@konard konard self-assigned this Sep 14, 2025
konard and others added 2 commits September 14, 2025 15:20
- Add AutoDispose attribute for marking classes that need automatic disposal
- Create AutoDisposeSourceGenerator that generates disposal code at compile-time
- Automatically dispose all IDisposable and Platform.Disposables.IDisposable fields
- Handle readonly fields correctly by skipping null assignment
- Add comprehensive tests demonstrating functionality
- Update project version to 0.5.0
- Fix existing test compatibility with net8

Resolves #2

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] Automatic dispose code generation Implement automatic dispose code generation Sep 14, 2025
@konard konard marked this pull request as ready for review September 14, 2025 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Automatic dispose code generation

1 participant